package jp.michi.makimono;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory.Options;
import android.util.Log;

public class Mipmap {
    public static void prepare(String fileName) throws IOException {
        RandomAccessFile randomAccessFile = null;
        Bitmap originalBitmap = null;
        Bitmap resizedBitmap = null;
        OutputStream outputStream = null;
        try {
            long time = System.nanoTime();
            randomAccessFile = new RandomAccessFile(fileName, "r");
            {
                Log.v("test", "fopen " + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6));
                time = System.nanoTime();
            }
            Options options = new Options();
            options.inInputShareable = true;
            options.inPurgeable = true;
            originalBitmap = BitmapFactory.decodeFileDescriptor(randomAccessFile.getFD(), null, options);
            {
                Log.v("test", "decode " + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6));
                time = System.nanoTime();
            }
            {
                float zoom = 0.5F;
                Matrix matrix = new Matrix();
                matrix.setScale(zoom, zoom);
                resizedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);
            }
            //            {
            //                Bitmap b = Bitmap.createBitmap(options.outWidth / 2, options.outHeight / 2, Config.RGB_565);
            //                Canvas c = new Canvas(b);
            //                Paint paint = new Paint();
            //                paint.setFilterBitmap(true);
            //                Matrix matrix = new Matrix();
            //                float zoom = 0.5F;
            //                matrix.setScale(zoom, zoom);
            //                c.drawBitmap(originalBitmap, matrix, paint);
            //                resizedBitmap = b;
            //            }
            {
                Log.v("test", "createBitmap " + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6));
                time = System.nanoTime();
            }
            //            resizedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);
            //            resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, options.outWidth / 2, options.outHeight / 2, true);
            //            resizedBitmap.getWidth();
            //            resizedBitmap.getHeight();
            {
                outputStream = new FileOutputStream("/sdcard/_my/test/tmp.jpg");
                resizedBitmap.compress(CompressFormat.JPEG, 100, outputStream);
                Log.v("test", "jpg compress " + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6));
                time = System.nanoTime();
            }
            {
                outputStream = new FileOutputStream("/sdcard/_my/test/tmp.png");
                resizedBitmap.compress(CompressFormat.PNG, 100, outputStream);
                Log.v("test", "png compress " + BigDecimal.valueOf(System.nanoTime() - time).movePointLeft(6));
                time = System.nanoTime();
            }
            resizedBitmap.recycle();
            outputStream.flush();
            outputStream.close();
            outputStream = null;
        } finally {
            Closer.close(randomAccessFile, outputStream);
        }
    }
}
